#include "AuServer.h"
#include "MessageDto.h"
#include "TcpSocket.h"
#include "Message.h"
#include <iostream>
#include "RequestDispatcher.h"
#include <sys/epoll.h>
using namespace std;

void func(gpointer data,gpointer user_data){
    MessageDto* dto=(MessageDto*)data;
    TcpSocket* socket=dto->socket;
    //收信息
    Message* msg=socket->receiveMag(3000);
    char* content=msg->content;
    cout<<msg->content<<endl;
    int len=msg->length;
    if (len==-1)
    {
        cout<<"读取失败"<<endl;
    }else if (len==0)
    {
        cout<<"客户端关闭"<<endl;
        epoll_ctl(dto->epollId,EPOLL_CTL_DEL,socket->getConnFd(),NULL);
        close(socket->getConnFd());
    }else
    {
        RequestDispatcher dispatcher;
        dispatcher.dispatch(socket,content);
    }
    delete dto;
}

AuServer::AuServer(int port)
{
    this->server=new TcpServer(port);
}

void AuServer::start()
{
    this->server->startWithPool(func);
}

AuServer::~AuServer()
{
    if (this->server!=nullptr)
    {
        delete this->server;
    }
    
}
